﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace LedDemo
{
    public static class IniFile
    {
        public static readonly string m_strFileName = "ComCfg.ini";

        private static string _appDirectory = "";
        public static string AssemblyDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(_appDirectory))
                {
                    string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                    _appDirectory = directory.Substring(6);
                }
                return _appDirectory;
            }
        }

        static IniFile()
        {
            m_strFileName = Path.Combine(AssemblyDirectory, "ComCfg.ini");
        }

        #region 读取INI文件
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string Section, string IniKey)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, IniKey, "", temp, 500, m_strFileName);
            return temp.ToString();
        }

        /// <summary>
        /// 读取INI文件(找不到配置文件返回默认值)
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string Section, string IniKey,string strDefault,string iniFileName)
        {
            if (string.IsNullOrEmpty(iniFileName))
            {
                iniFileName = m_strFileName;
            }            
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, IniKey, strDefault, temp, 500, iniFileName);
            return temp.ToString();
        }

        public static string IniValue(string section, string key, string def, string fileName, string encodingName, int size = 1024)
        {
            byte[] buffer = new byte[size];
            int count = GetPrivateProfileString(
                section,
                key,
                def,
                buffer,
                size,
                fileName);
            return Encoding.GetEncoding(encodingName).GetString(buffer, 0, count).Trim();
        }

        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);


        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        //与ini交互必须统一编码格式
        private static byte[] getBytes(string s, string encodingName)
        {
            return null == s ? null : Encoding.GetEncoding(encodingName).GetBytes(s);
        }

        #region 写INI文件
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static long WriteValue(string Section, string IniKey, string NewValue)
        {
            long i = WritePrivateProfileString(Section, IniKey, NewValue, m_strFileName);
            return i;
        }

        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static long WriteValue(string Section, string IniKey, string NewValue, string iniFileName)
        {
            if (string.IsNullOrEmpty(iniFileName))
            {
                iniFileName = m_strFileName;
            }
            long i = WritePrivateProfileString(Section, IniKey, NewValue, iniFileName);
            return i;
        }

        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(
            string section, string key, string val, string filePath);
        #endregion
    }
}